import { promisify } from '../../../utils/promise.util'
import { $init, $digest } from '../../../utils/common.util'

var tab
var app = getApp();
Page({
  data: {
    showtab:0,  //顶部选项卡索引
    showtabtype:'', //选中类型
    showfootertab:0,  //底部标签页索引
    tabnav:{},  //顶部选项卡数据
    questionsall:[],  //所有问题
    questions:[], //问题列表
    showquestionindex:null, //查看问题索引,
    uploadimgs:[], //上传图片列表
    editable: false, //是否可编辑
    showView: true,
    array: [{ name: '丢失东西'}, {name: '捡到东西'}],
    images: [],
    tab: 0,
    inputTheme: "",
    inputDescrible:"",
    inputName:"",
    inputPhone:"",
  },

  inputDescrible: function (e) {
    var val = e.detail.value;
    this.setData({
      inputDescrible: val
    });
  },
  inputTheme:function (e){
    var val = e.detail.value;
    this.setData({
      inputTheme: val
    });
  },
  inputName: function (e) {
    var val = e.detail.value;
    this.setData({
      itemName: val
    });
  },
  inputPhone: function (e) {
    var val = e.detail.value;
    this.setData({
      inputPhone: val
    });
  },

  choseTxtColor: function (e) {
    var tab = e.currentTarget.dataset.id;  //获取自定义的ID值
    this.setData({
    tab: tab
    })
  },

  chooseImage(e) {
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const images = this.data.images.concat(res.tempFilePaths)
        this.data.images = images.length <= 3 ? images : images.slice(0, 3)
        console.log(this.data.images)
        console.log('一共' + this.data.images.length + '张图片')
        $digest(this)
      }
    })
  },

  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.images.splice(idx, 1)
    $digest(this)
  },

  handleImagePreview: function (e) {
    const idx = e.target.dataset.idx
    const images = this.data.images

    wx.previewImage({
      current: images[idx],
      urls: images,
    })
  },


  callContact: function(e){  //电话
    wx.makePhoneCall({
      phoneNumber: '82904110'
    })
  },


  lostSubmit: function () {
    var that = this;
    var id = 0;
    var uuid = "";
    var flag = "";
    var url = 'http://120.77.36.131:8180/STULive/uploadLostFound.action';
    var path = that.data.images;

    this.uploadImgs({
      url: url,
      path: path,
      id: id,
      uuid: uuid,
      flag: flag,

    });
    wx.showToast({
      title: '发布成功！',
    });
  },

  uploadImgs: function (data) {
    var that = this;
    var i = data.i ? data.i : 0;
    var judge = (i == data.path.length - 1);
    var success = data.success ? data.success : 0;
    var fail = data.fail ? data.fail : 0;
    data.flag = judge ? "ok" : "";
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file',
      formData: {
        rdSession: app.globalData.rdSession,
        type: that.data.tab,
        title: that.data.inputTheme,
        content: that.data.inputDescrible,
        contact: that.data.inputName,
        telephone: that.data.inputPhone,
        uuid: data.uuid,
        flag: data.flag,
      },
      success: (res) => {
        console.log('第' + i + '张上传成功');
        console.log(res.data);
        i++;
        if (i == data.path.length) {
          console.log('上传完毕');

          data.id = JSON.parse(res.data).data;
          that.data.itemID = data.id;
          console.log('id为：' + data.id);

        } else {
          //console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.uuid = JSON.parse(res.data).data;
          that.uploadImgs(data);

        }
        success++;
      },
      fail: (res) => {
        fail++;
        console.log('fail:' + i + 'fail:' + fail)
      },
      complete: (res) => {

      }
    })
12
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    $init(this)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
